package com.gadgets.cm01.role.controller.impl;

import com.gadgets.cm01.auth.dto.AuthorityTreeDTO;
import com.gadgets.cm01.auth.pojo.Authority;
import com.gadgets.cm01.common.GadgetsCm01Constant;
import com.gadgets.cm01.common.GadgetsCm01ExceptionConstant;
import com.gadgets.cm01.role.controller.IRoleAuthAllocationController;
import com.gadgets.cm01.role.dto.RoleAuthAllocationDTO;
import com.gadgets.cm01.role.dto.RoleAuthQueryDTO;
import com.gadgets.cm01.role.pojo.Role;
import com.gadgets.cm01.role.pojo.RoleAuthorityRelation;
import com.gadgets.cm01.role.service.IRoleAuthAllocationService;
import com.gadgets.framework.annotation.RoleAuthCheck;
import com.gadgets.framework.common.constant.GadgetsConstant;
import com.gadgets.framework.common.dto.HttpBaseResp;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.util.CacheUtils;
import com.gadgets.framework.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-10-10
 *
 */
@RestController
@RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/roleAuthAllocation")
public class RoleAuthAllocationControllerImpl implements IRoleAuthAllocationController {

    @Autowired
    @Qualifier(value = "roleAuthAllocationServiceImpl")
    private IRoleAuthAllocationService roleAuthAllocationService;
    @Autowired
    private CacheUtils cacheUtils;

    /**
     * 角色权限分配
     *
     * @param request           请求信息
     * @param response          应答信息
     * @param authAllocationDTO 权限操作信息
     *
     * @throws GadgetsServiceException 异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/roleAuthAllocation.do")
    @RoleAuthCheck
    @Override
    public HttpBaseResp<Object> roleAuthAllocation(HttpServletRequest request, HttpServletResponse response, RoleAuthAllocationDTO authAllocationDTO) throws GadgetsServiceException {
        if(GadgetsCm01Constant.ROLE_AUTH_OPER_TYPE.ALLOCATION.equals(authAllocationDTO.getOperType())){
            // 为角色分配菜单
            roleAuthAllocationService.allocationRoleAuth(authAllocationDTO);
        }else {
            // 为角色取消菜单分配
            roleAuthAllocationService.reAllocationRoleAuth(authAllocationDTO);
        }
        return new HttpBaseResp<>();
    }

    /**
     * 查询菜单归属角色
     *
     * @param request    请求信息
     * @param response   应答信息
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     *
     * @return 角色列表
     *
     * @throws GadgetsServiceException 异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/selectRelationRole.do")
    @RoleAuthCheck
    @Override
    public HttpBaseResp<RoleAuthorityRelation> selectAuthRoleByCondition(HttpServletRequest request, HttpServletResponse response, RoleAuthQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException {
        if(!StringUtils.isBlank(queryDTO.getRoleIds())){
            queryDTO.setRoleIdsArr(StringUtils.splitStrToLong(queryDTO.getRoleIds()));
        }
        List<RoleAuthorityRelation> roleAuthorityRelationList = roleAuthAllocationService.queryAuthRoleByCondition(queryDTO,pagination);
        return new HttpBaseResp<>(roleAuthorityRelationList,pagination);
    }

    /**
     * 角色下菜单信息查询
     *
     * @param request  请求信息
     * @param response 应答信息
     * @param queryDTO 查询条件
     *
     * @return 菜单列表
     *
     * @throws GadgetsServiceException 异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/selectRelationAuthority.do")
    @RoleAuthCheck
    @Override
    public HttpBaseResp<Authority> selectAuthorityListByCondition(HttpServletRequest request, HttpServletResponse response, RoleAuthQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException {
        List<Authority> roleAuthorityRelationList = roleAuthAllocationService.queryRoleAuthInfoByCondition(queryDTO,pagination);
        return new HttpBaseResp<>(roleAuthorityRelationList,pagination);
    }

    /**
     * 角色下菜单树信息查询
     *
     * @param request  请求信息
     * @param response 应答信息
     * @param queryDTO 查询条件
     *
     * @return 菜单列表
     *
     * @throws GadgetsServiceException 异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/selectRelationAuthorityTree.do")
    @RoleAuthCheck
    @Override
    public AuthorityTreeDTO selectAuthrityTreeByCondition(HttpServletRequest request, HttpServletResponse response, RoleAuthQueryDTO queryDTO) throws GadgetsServiceException {
        List<String> orderColumns = new ArrayList<>();
        orderColumns.add("ID");
        orderColumns.add("AUTH_ORDER");
        queryDTO.setOrderColumn(orderColumns);
        List<Authority> roleAuthorityRelationList = roleAuthAllocationService.queryRoleAuthInfoByCondition(queryDTO,null);
        return new AuthorityTreeDTO(roleAuthorityRelationList);
    }

    /**
     * 用户查询菜单信息
     *
     * @param request  请求信息
     * @param response 应答信息
     * @param queryDTO 查询条件
     * @return 菜单树
     * @throws GadgetsServiceException 异常信息
     */
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/selectUserAuthority.do")
    @RoleAuthCheck
    @SuppressWarnings("unchecked")
    @Override
    public AuthorityTreeDTO selectAuthorityTreeForUser(HttpServletRequest request, HttpServletResponse response, RoleAuthQueryDTO queryDTO) throws GadgetsServiceException {
        String sessionId = null;
        if(null!=request.getAttribute(GadgetsConstant.USER_SESSION_ID_KEY)) {
            sessionId = request.getAttribute(GadgetsConstant.USER_SESSION_ID_KEY).toString();
        }else {
            // 未能获取到Cookie当中的SessionId，抛出异常
            throw new GadgetsServiceException(GadgetsCm01ExceptionConstant.NOT_LOGIN,new Object[]{});
        }
        HashMap<String,Object> sessionCache = cacheUtils.getUserSessionCache(sessionId);
        List<Role> list = (List<Role>) sessionCache.get(GadgetsConstant.SESSION_CACHE_KEY.ROLE_LIST);
        String roleIds = "";
        for (Role role : list) {
            roleIds = role.getId()+",";
        }
        roleIds = roleIds.substring(0,roleIds.length()-1);
        queryDTO.setIfTree(GadgetsConstant.NUMBER_CONSTANT.STR_NUMBER_1);
        queryDTO.setRoleIds(roleIds);
        return selectAuthrityTreeByCondition(request, response, queryDTO);
    }
}
